﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using HotelManagement.DAO;

namespace Check_out
{
    public partial class Check_out : Form
    {
        HotelManagement.DAO.CheckoutDAO m_dao;

        public Check_out()
        {
            InitializeComponent();

            m_dao = new CheckoutDAO();
        }

        private void search(object sender, EventArgs e)
        {
            m_dao.search(searchRoomCode.Text, this);

            totalPrice.Text = (int.Parse(totalRoomFee.Text) + int.Parse(totalServiceFee.Text)).ToString();

            customerCode.Invalidate();
        }

        public void setCustomerCode(string code)
        {
            customerCode.Text = code;
        }

        public void setCustomerName(string name)
        {
            customerName.Text = name;
        }

        public void setRoomCode(string code)
        {
            roomCode.Text = code;
        }

        public void setRoomPrice(int price)
        {
            roomPrice.Text = price.ToString();
        }

        public void setInDate(DateTime date)
        {
            inDate.Text = date.ToShortDateString();
        }

        public void computeTotalRoomFee()
        {
            int rentDays = (DateTime.Today - DateTime.Parse(inDate.Text)).Days;
            totalRoomFee.Text = (rentDays * int.Parse(roomPrice.Text)).ToString();
        }

        public void setService(DataTable services)
        {
            ServiceBillsList.DataBindings.Clear();
            ServiceBillsList.DataSource = services;

            int totalServicePrice = new int();
            totalServicePrice = 0;
            for (int i = 0; i < ServiceBillsList.RowCount - 1; ++i)
                totalServicePrice += int.Parse(ServiceBillsList.Rows[i].Cells["TongSoTien"].Value.ToString());

            totalServiceFee.Text = totalServicePrice.ToString();
        }

        private void getServiceNote(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView data = sender as DataGridView;
            if (data == null)
                return;

            if (data.CurrentRow.Selected && data.CurrentRow.Index < (data.Rows.Count - 1))
            {
                serviceNote.Text = m_dao.getNote(roomCode.Text, customerCode.Text, data.CurrentRow.Cells["MaDV"].Value.ToString());
                serviceNote.Invalidate();
            }
        }

        private void quitForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChecIfOut(object sender, EventArgs e)
        {
            if (int.Parse(totalPrice.Text) < int.Parse(paid.Text))
            {
                m_dao.checkOut(roomCode.Text);
                MessageBox.Show("Successfully checked out.");
            }
            else
            {
                MessageBox.Show("Not paid enough.");
            }
        }
    }
}
